package com.ijtunes.utils;

import com.ijtunes.filetypes.Track;
import com.ijtunes.sources.TrackSource;
import java.text.DecimalFormat;

/**
 * Records the number of actions taken.
 * @author Michael Sena
 * @version 1.0
 * @date 02/07/2010
 */
public class Actions {
    private static int[] actions = new int[Action.values().length];
    private static final DecimalFormat FORMATTER = new DecimalFormat("#00.00%");
    private static boolean showedTrackInfo = false;
    
    public static void logAction(Action action, TrackSource source, Track track, String extra) {
        actions[action.ordinal()]++;
        
        if (!showedTrackInfo && track != null) {
            if (Properties.getBool(Property.VERBOSE_OUTPUT)) {
                String percent = FORMATTER.format(
                    (double) source.getUpTo() / source.size());
                System.out.print("[" + percent + " - " + source.getUpTo()
                    + "/" + source.size() + "]");
            }
            try {
                System.out.print(track.toString() + ": ");
            } catch (NullPointerException npe) {
                Logger.log(Actions.class.getName(),
                    "[error getting track string: " + track.getLocation(),
                    npe);
            }
            showedTrackInfo = true;
        }

        if (extra == null) {
            extra = "";
        }
        System.out.print(" -" + action.toString() + (!extra.isEmpty() ? "[" + extra + "]" : "."));
    }
    
    public static void finishingActions() {
        if(showedTrackInfo) {
            System.out.println();
            showedTrackInfo = false;
        }
    }
    
    public static String generateSummary() {
        StringBuilder toDisplay = new StringBuilder();
        toDisplay.append("Results").append(System.lineSeparator());
        toDisplay.append("=======").append(System.lineSeparator());
        for(int i = 0; i < Action.values().length; i++) {
            Action action = Action.values()[i];
            toDisplay
                .append(action.getSummaryName())
                .append(": ")
                .append(actions[i])
                .append(System.lineSeparator());
        }
        return toDisplay.toString();
    }
}
